package com.gz.common.mongodb.support;

import com.gz.common.mongodb.utils.Mongo;
import com.gz.common.mongodb.base.MongoBaseMapper;
import com.gz.common.mongodb.base.MongoBaseModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.lang.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MongoPage
 * @Description //
 * @Author WHX
 * @Date 2021/8/20 18:05
 **/
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class MongoPage<Model> {
    /**
     * 页码，起始页：1
     */
    private Integer page;
    /**
     * 每页条数
     */
    private Integer pageSize;
    /**
     * 总条数
     */
    private Long count;
    /**
     * 总页数
     */
    private Integer pages;

    /**
     * 数据
     */
    private List<Model> list;

    /**
     * 分页查询
     *
     * @param dao      dao层接口
     * @param query    查询条件（可携带排序规则）
     * @param page     起始页：1
     * @param pageSize 每页条数
     * @return
     */
    public static <Model, ID> MongoPage<Model> getPage(MongoBaseMapper<Model, ID> dao, Query query, Integer page, Integer pageSize) {
        MongoPage mongoPage = MongoPage.startPage(page, pageSize);

        // 查询数量
        Long count = dao.count(query);
        mongoPage.setCount(count);

        // 判断是否还需要继续分页查询
        if (count == 0 || pageSize < 1) {
            return mongoPage.setPages(0).setList(new ArrayList());
        }

        // 页码处理
        int pages = (int) Math.ceil((double) count / (double) pageSize);
        page = page < 1 ? 1 : (page > pages ? pages : page);

        // 分页查询 （mongo的起始页为0）
        Pageable pageable = PageRequest.of(page - 1, pageSize);
        List<Model> list = dao.find(query.with(pageable));

        return mongoPage.setPages(pages).setPage(page).setList(list);
    }

    /**
     * 分页查询 + 自动构建query条件
     * @param dao
     * @param model
     * @param page
     * @param pageSize
     * @param <Model>
     * @param <ID>
     * @return
     */
    public static  <Model, ID> MongoPage<Model> getPage(MongoBaseMapper<Model, ID> dao, @Nullable MongoBaseModel model, Integer page, Integer pageSize) {
        return getPage(dao, model, null, page, pageSize);
    }

    /**
     * 分页查询 + 自动构建query条件 + 排序
     * @param dao
     * @param model
     * @param sort
     * @param page
     * @param pageSize
     * @param <Model>
     * @param <ID>
     * @return
     */
    public static  <Model, ID> MongoPage<Model> getPage(MongoBaseMapper<Model, ID> dao, @Nullable MongoBaseModel model, @Nullable Sort sort, Integer page, Integer pageSize) {
        return getPage(dao, Mongo.getSelectiveCriteria(model), sort, page, pageSize);
    }

    /**
     * 分页查询 + 自定义查询条件 + 排序
     * @param dao
     * @param criteria
     * @param sort
     * @param page
     * @param pageSize
     * @param <Model>
     * @param <ID>
     * @return
     */
    public static  <Model, ID> MongoPage<Model> getPage(MongoBaseMapper<Model, ID> dao, Criteria criteria, @Nullable Sort sort, Integer page, Integer pageSize) {
        Query query = Query.query(criteria);
        if (sort != null) query.with(sort);
        return getPage(dao, query, page, pageSize);
    }

    /**
     * 开始分页
     *
     * @param page  页码
     * @param pageSize 每页显示数量
     */
    public static MongoPage startPage(Integer page, Integer pageSize) {
        MongoPage mongoPage = new MongoPage().setPage(page).setPageSize(pageSize);
        return mongoPage;
    }

}
